﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_Entities;
using CoolERP_BLL;

namespace CoolERP_Web.User
{
    public partial class ZL_KH_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    this.liTitle.Text = "修改";
                    this.hdId.Value = Request.QueryString["id"];
                    BindData();
                }
            }
        }

        private void BindData()
        {
            using (ZL_KHBLL zBLL = new ZL_KHBLL())
            {
                ZL_KH entity = zBLL.GetEntity(t => t.ID == new Guid(this.hdId.Value));
                this.tbBH.Text = entity.BH.Trim();
                this.tbBH.Enabled = false;
                this.tbMC.Text = entity.MC;
                this.tbJC.Text = entity.JIANCHENG;
                this.tbJC.Enabled = false;
                this.tbFRDB.Text = entity.FRDB;
                this.tbGSDH.Text = entity.GSDH;
                this.tbGSCZ.Text = entity.GSCZ;
                this.tbBZ.Text = entity.BZ;
                this.tbDZ.Text = entity.DZ;
                this.tbLXR.Text = entity.LXR;
                this.tbLXRCZ.Text = entity.LXRCZ;
                this.tbLXRDH.Text = entity.LXRDH;
                this.tbLXRSJ.Text = entity.LXRSJ;
                if (entity.ShiID != null)
                    this.AjaxWebJavaScript(string.Format("area.setArea({0});", entity.ShiID));
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            using (ZL_KHBLL zBLL = new ZL_KHBLL())
            {
                ZL_KH entity = new ZL_KH();
                if (!string.IsNullOrEmpty(this.hdId.Value))
                    entity = zBLL.GetEntity(t => t.ID == new Guid(this.hdId.Value));
                else
                {
                    entity.ID = Guid.NewGuid();
                    entity.BH = this.tbBH.Text.Trim();
                    entity.JIANCHENG = this.tbJC.Text.Trim();
                }
                entity.MC = this.tbMC.Text.Trim();
                entity.FRDB = this.tbFRDB.Text.Trim();
                entity.GSDH = this.tbGSDH.Text.Trim();
                entity.GSCZ = this.tbGSCZ.Text.Trim();
                entity.LXR = this.tbLXR.Text.Trim();
                entity.LXRCZ = this.tbLXRCZ.Text.Trim();
                entity.LXRDH = this.tbLXRDH.Text.Trim();
                entity.LXRSJ = this.tbLXRSJ.Text.Trim();
                if (Request.Form["selProvince"] != null)
                    entity.ShengID = Convert.ToInt32(Request.Form["selProvince"]);
                if (Request.Form["selCity"] != null)
                    entity.ShiID = Convert.ToInt32(Request.Form["selCity"]);
                entity.DZ = this.tbDZ.Text.Trim();
                entity.BZ = this.tbBZ.Text.Trim();
                int result = zBLL.SaveEntity(entity, t => t.ID != entity.ID && t.BH == entity.BH && t.MC == entity.MC);
                if (result == -1)
                    lbResult.Text = "已存在相同的编号或者客户名称";
                else if (result == 1)
                    lbResult.Text = "保存成功";
                else
                    lbResult.Text = "保存失败";
                this.AjaxWebJavaScript(string.Format("area.setArea({0});", entity.ShiID));
            }
        }
    }
}